import axios from "axios";
import axiosInstance from "./interceptors";
const baseUrl = "/gateway";
let source: any;

// 取消上一次请求
function cancelRequest() {
  if (typeof source == "function") {
    source("终止请求");
  }
}
function get(url, params = {}, headers: {}) {
  return axiosInstance({
    method: "get",
    url: baseUrl + url,
    params,
    headers: {
      "Content-Type": "application/json",
      charset: "utf-8",
      ...headers,
    },
    cancelToken: new axios.CancelToken(function executor(c) {
      source = c;
    }),
  });
}

function getExport(url, params = {}, headers: {}) {
  return axiosInstance({
    method: "get",
    url: baseUrl + url,
    params,
    responseType: "blob",
    headers: {
      "Content-Type": "application/json",
      charset: "utf-8",
      ...headers,
    },
    cancelToken: new axios.CancelToken(function executor(c) {
      source = c;
    }),
  });
}

function logExport(url, data = {}, headers: {}) {
  return axiosInstance({
    method: "post",
    url: baseUrl + url,
    data,
    responseType: "blob",
    headers: { "Content-Type": "application/json", charset: "utf-8" },
    cancelToken: new axios.CancelToken(function executor(c) {
      source = c;
    }),
  });
}
function post(url, data, headers = {}) {
  return axiosInstance({
    method: "post",
    url: baseUrl + url,
    headers: { "Content-Type": "application/json", ...headers },
    data,
  });
}

// params传参方式
function postItem(url, params, headers = {}) {
  return axiosInstance({
    method: "post",
    url: baseUrl + url,
    headers: { "Content-Type": "application/json", ...headers },
    params,
  });
}

function put(url, data, headers = {}) {
  return axiosInstance({
    method: "put",
    url: baseUrl + url,
    headers: { "Content-Type": "application/json", ...headers },
    data,
  });
}

function putItem(url, params = {}, headers = {}) {
  return axiosInstance({
    method: "put",
    url: baseUrl + url,
    headers: { "Content-Type": "application/json", ...headers },
    params,
  });
}

function deleteItem(url, params = {}, headers = {}) {
  return axiosInstance({
    method: "delete",
    url: baseUrl + url,
    headers: { "Content-Type": "application/json", ...headers },
    params,
  });
}

function deleteBatch(url, data, headers = {}) {
  return axiosInstance({
    method: "delete",
    url: baseUrl + url,
    headers: { "Content-Type": "application/json", ...headers },
    data,
  });
}

// ---------------------------以下 为无公用前缀baseUrl的请求封装装
function getTemp(url, params = {}) {
  return axiosInstance({
    method: "get",
    url: url,
    params,
  });
}

function postTemp(url, data, headers = {}) {
  return axiosInstance({
    method: "post",
    url: url,
    headers: { "Content-Type": "application/json", ...headers },
    data,
  });
}
function deleteTemp(url, params = {}) {
  return axiosInstance({
    method: "delete",
    url: url,
    params,
  });
}
function getExportTemp(url, params = {}, headers: {}) {
  return axiosInstance({
    method: "get",
    url: url,
    params,
    responseType: "blob",
    headers: {
      "Content-Type": "application/json",
      charset: "utf-8",
      ...headers,
    },
    cancelToken: new axios.CancelToken(function executor(c) {
      source = c;
    }),
  });
}
// ----------------------------------

export default {
  get: (url, params, headers = {}) => get(url, params, headers),
  getExport: (url, params, headers = {}) => getExport(url, params, headers),
  logExport: (url, params, headers = {}) => logExport(url, params, headers),
  post: (url, params, headers = {}) => post(url, params, headers),
  put: (url, params, headers = {}) => put(url, params, headers),
  delete: (url, params) => deleteItem(url, params),
  deleteBatch: (url, params) => deleteBatch(url, params),
  putItem: (url, params) => putItem(url, params),
  postItem: (url, params, headers = {}) => postItem(url, params, headers),

  // 无公用前缀
  getTemp: (url, params) => getTemp(url, params),
  postTemp: (url, params, headers = {}) => postTemp(url, params, headers),
  deleteTemp: (url, params) => deleteTemp(url, params),
  getExportTemp: (url, params, headers = {}) =>
    getExportTemp(url, params, headers),
  cancelRequest: () => cancelRequest(),
};
